package com.my.assembly.http;

import android.util.Log;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;

/**
 * <pre>
 *     author : wxq
 *     time   : 2018/12/10
 *     desc   :
 *     version: 1.0
 * </pre>
 */
public class HttpClient {

    private static HttpClient httpClient = new HttpClient();

    private static final int TIMEOUT = 30;

    private OkHttpClient okHttpClient;

    private HttpClient() {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Log.e("OKHTTP", "-------->" + message);
            }
        });
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(logging)
                .connectTimeout(TIMEOUT, TimeUnit.SECONDS) // 设置超时时间
                .writeTimeout(TIMEOUT, TimeUnit.SECONDS) // 写入超时
                .readTimeout(TIMEOUT, TimeUnit.SECONDS) // 读取超时
                .build();
    }

    public static HttpClient getInstance() {
        return httpClient;
    }

    public OkHttpClient getClient() {
        return okHttpClient;
    }
}
